#!/usr/bin/env python    
#encoding: utf-8  
import sys, getopt
import myutils

input_file = r'input.in'
output_file = r'output.out'
version = '0.2'
bin2coe_flag = False
bin2IntelHex_flag = False
bin2tbMem_flag    = False
help_msg = \
"""
[*] Help message
-i : input  file exp: inst.bin
-o : output file exp: inst.coe
-f : bin2coe(b2c) bin2IntelHex(b2i) bin2TbMem (b2t)
-v : version
-h : help msg
"""

try:
    opts,args = getopt.getopt(sys.argv[1:],'-h-v-i:-o:-f:',['help','version','input','output','format'])
except getopt.GetoptError:
    print('-i <input_file> -o <output_file> -f <format>')

for opt_name , opt_value in opts:
    if opt_name in ('-v','--version'):
        print('Version=' + version)
        sys.exit()
    elif opt_name in ('-h','--help'):
        print(help_msg)
        sys.exit()
    elif opt_name in ('-i','--input'):
        input_file = opt_value
    elif opt_name in ('-o','--output'):
        output_file = opt_value
    elif opt_name in ('-f','--format'):
        option = opt_value
        if option == 'b2c':
            bin2coe_flag = True
        elif option == 'b2i':
            bin2IntelHex_flag = True
        elif option == 'b2t':
            bin2tbMem_flag = True

if bin2coe_flag:
    myutils.bin2coe(input_file,output_file)
    print("bin to coe , Successful!!!")
elif bin2IntelHex_flag:
    myutils.bin2IntelHex(input_file,output_file)
    print("bin to IntelHex , Successful!!!")
elif bin2tbMem_flag:
    myutils.bin2tbMem(input_file,output_file)
    print("bin to tb mem, Successful!!!")
else:
    print("please input useful opt....")
    print(help_msg)